<?php
namespace action\forum;
use \cass\formulaire AS form,
    \cass\Cache;

class forumAction extends \cass\Action {

    /**
     * Méthode de cache
     * @return Integer Type de cache (Cache::<type>)
     */
    public function cache() {
        return array(
            'execute' => Cache::SESSION_MULTIPLE
        );
    }

    /**
     * Méthode d'affichage du forum
     * @param $_forum Id du forum
     * @param $_forumName Nom slug du forum
     */
    public function execute($_forum, $_forumName, $_page = 1) {
        $forum = $this->doctrine->find('Forum', $_forum);
        $this->forwardEven($forum->hasCredential($this->getSession()), '@403');

        $this->setTitle($forum->getTitle());
        $this->addToAriane('Forums', url_for('forum'));

        
        $query = $this->doctrine->createQuery('SELECT d FROM ForumsDiscussion d JOIN d.forum f WHERE f.id = :forum ORDER BY d.updated_at DESC, d.created_at DESC')
                        ->setParameter('forum', $_forum);

        $pagination = new \cass\Pagination($query);
        $pagination->setUri(\cass\Uri::create('forum', 'forum', null, array($_forum, $_forumName)));
        $pagination->setCurrentPage($_page);
        $pagination->setNbByPage(\cass\Data::config('forum')->nb_discussions);

        $this->forum = $forum;
        $this->pagination = $pagination;
        $this->discussions = $pagination->getElements();
    }

    /**
     * Créer une discussion dans le forum
     * @param $_forum Id du forum
     * @param $_forumName Nom slug du forum
     */
    public function executeCreateDiscussion($_forum, $_forumName) {
        $forum = $this->doctrine->find('Forum', $_forum);
        $this->forwardEven($forum->hasCredential($this->getSession()), '@403');

        $this->setTitle('Nouvelle discussion');
        $this->addToAriane('Forums', url_for('forum'));
        $this->addToAriane($forum->getTitle(), url_for('forum', 'forum', null, array($_forum, $_forumName)));

        $formulaire = new form\Formulaire('Nouvelle discussion');
        $formulaire->setButtons('submit', 'Poster la nouvelle discussion');
        $formulaire->addToLinks('Retour au forum', url_for('forum', 'forum', null, array($_forum, $_forumName)));
        $formulaire->append(new form\Input('Titre de la discussion', '', true, 'Soyez explicite'));
        $formulaire->append(new form\Editor('Message', '', true, 'Relisez-vous bien, n\'utilisez pas le SMS et évitez les fautes évidentes'));

        // Vérifications
        if ($formulaire->isPosted()) {
            
        }

        // Enregistrements
        if ($formulaire->isPostedWithoutError()) {
            $discussion = new \ForumsDiscussion;
            $discussion->setCreatedAt(new \DateTime('now'));
            $discussion->setUpdatedAt(new \DateTime('now'));
            $discussion->setTitle($formulaire->getValue('Titre de la discussion'));
            $discussion->setUser($this->getSession()->getUser());
            $discussion->setForum($forum);
            $discussion->getReaders()->add($this->getSession()->getUser());
            $this->doctrine->persist($discussion);

            $message = new \ForumsMessage;
            $message->setUser($this->getSession()->getUser());
            $message->setCreatedAt(new \DateTime('now'));
            $message->setDiscussion($discussion);
            $message->setContent($formulaire->getValue('Message'));
            $this->doctrine->persist($message);
            
            $this->doctrine->flush();

            $formulaire->setConfirm('Le nouveau sujet de discussion a été crée, vous pouvez <a href="'.url_for('forum', 'discussion', null, array($discussion->getId(), slugify($discussion->getTitle()))).'">voir votre message</a>.');
        }

        $this->formulaire = $formulaire;
    }

    /**
     * Marquer tous les messages comme lus
     * @param $_forum Id du forum à marquer comme lu
     * @param $_forumName Nom slug du forum
     */
    public function executeRead($_forum, $_forumName) {
        $forum = $this->doctrine->find('Forum', $_forum);
        foreach ($forum->getDiscussions() AS $discussion) {
            $discussion->read($this->getSession());
            $this->doctrine->persist($discussion);
        }

        $this->doctrine->flush();
        $this->redirect('forum', 'forum', null, array($_forum, $_forumName));
    }

}